package <%= Configuration.package %>.<%= Configuration.controllers_package %>;

import java.util.List;

import <%= Configuration.full_models_package %>.<%= class_name %>;
import <%= Configuration.package %>.<%= Configuration.repositories_package %>.<%= repository_interface_name %>;
<%- @attributes.each do |att| -%>
	<%- if att.type.eql?("references") -%>
import <%= Configuration.package %>.repositories.<%= att.name.camelize %>Repository;		
	<%- end -%>
<%- end -%>
import br.com.caelum.vraptor.Delete;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Put;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.Validator;

@Resource
public class <%= controller_class_name %> {

	private final Result result;
	private final <%= repository_interface_name %> repository;
	<%- @attributes.each do |att| -%>
		<%- if att.type.eql?("references") -%>
	private final <%= att.name.camelize %>Repository <%= att.name %>Repository;		
		<%- end -%>
	<%- end -%>	
	private final Validator validator;
	
	public <%= controller_class_name %>(Result result, <%= repository_interface_name %> repository, 
	<% @attributes.each do |att| -%>
		<%- if att.type.eql?("references") -%><%= att.name.camelize %>Repository <%= att.name %>Repository,	<%- end -%><% end -%>Validator validator) {
		this.result = result;
		this.repository = repository;
		<%- @attributes.each do |att| -%>
			<%- if att.type.eql?("references") -%>
		this.<%= att.name %>Repository = <%= att.name %>Repository;<%- end -%><%- end -%>	
		this.validator = validator;
	}
	
	@Get("<%= path %>")
	public List<<%= class_name %>> index() {
		return repository.findAll();
	}
	
	@Post("<%= path %>")
	public void create(<%= class_name %> <%= model_parameter_name %>) {
		validator.validate(<%= model_parameter_name %>);
		validator.onErrorUsePageOf(this).new<%= class_name %>();
		repository.create(<%= model_parameter_name %>);
		result.redirectTo(this).index();
	}
	
	@Get("<%= "#{path}/new" %>")
	public <%= class_name %> new<%= class_name %>() {
		<%- @attributes.each do |att| -%>
			<%- if att.type.eql?("references") -%>
		result.include("<%=att.name%>List", <%= att.name %>Repository.findAll());		
			<%- end -%>
		<%- end -%>
		return new <%= class_name %>();
	}
	
	@Put("<%= path %>")
	public void update(<%= class_name %> <%= model_parameter_name %>) {
		validator.validate(<%= model_parameter_name %>);
		validator.onErrorUsePageOf(this).edit(<%= model_parameter_name %>);
		repository.update(<%= model_parameter_name %>);
		result.redirectTo(this).index();
	}
	
	@Get("<%= "#{path}/{#{model_parameter_name}.id}/edit" %>")
	public <%= class_name %> edit(<%= class_name %> <%= model_parameter_name %>) {
		<%- @attributes.each do |att| -%>
			<%- if att.type.eql?("references") -%>
		result.include("<%=att.name%>List", <%= att.name %>Repository.findAll());		
			<%- end -%>
		<%- end -%>		
		return repository.find(<%= model_parameter_name %>.getId());
	}

	@Get("<%= "#{path}/{#{model_parameter_name}.id}" %>")
	public <%= class_name %> show(<%= class_name %> <%= model_parameter_name %>) {
		return repository.find(<%= model_parameter_name %>.getId());
	}

	@Delete("<%= "#{path}/{#{model_parameter_name}.id}" %>")
	public void destroy(<%= class_name %> <%= model_parameter_name %>) {
		repository.destroy(repository.find(<%= model_parameter_name %>.getId()));
		result.redirectTo(this).index();  
	}
}